/*
* Copyright 2013-2020 Smartdot Technologies Co., Ltd. All rights reserved.
* SMARTDOT PROPRIETARY/CONFIDENTIAL. Use is subject to license
terms.
*
*/
package com.ssm.zhou.web;

import java.util.HashMap;
import java.util.Map;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import com.ssm.zhou.entity.User;
import com.ssm.zhou.exception.UserException;
import com.ssm.zhou.service.IUserService;

/**
*   注册Controller
* @author <a href="mailto:zhoujb@smartdot.com.cn">周竞博</a>
* @version 1.0, 2020年5月27日
*/
@Controller
@RequestMapping("/registered")
public class RegisteredController {
	private static Logger loger = LogManager.getLogger(LogManager.ROOT_LOGGER_NAME);
	@Autowired
	private IUserService userService;
	/**
	 * 注册跳转
	 * @return
	 */
	@RequestMapping("/jumpRegistered")
	public ModelAndView jumpRegistered() {
		ModelAndView mv = new ModelAndView();
		mv.setViewName("registered");
		return mv;
	}
	/**
	 * 执行注册方法
	 */
	@RequestMapping(value="/handlerRegistered",method=RequestMethod.POST)
	public ModelAndView handlerRegistered(User user){
		System.out.println(user);
		ModelAndView mv = new ModelAndView();
		String userName = user.getUserName();
		String password = user.getPassword();
		Map<String,String> errors = new HashMap<String,String>();
		mv.setViewName("registered");
		if(userName == null || userName.equals("") || userName.trim().isEmpty()){
			// 用户名不能为空
			errors.put("userName", "用户名不能为空");
		}
		if(password == null || password.equals("") || password.trim().isEmpty()){
			// 密码不能为空
			errors.put("password", "密码不能为空");
		}	
		if(errors != null && errors.size() > 0){
			mv.addObject("errors", errors);
			return mv;
		}
		try {
			userService.insertUser(user);
			mv.addObject("exceptionInfo","");	
		} catch (UserException e) {
			System.out.println(e.getMessage() + "controller");
			mv.addObject("exceptionInfo",e.getMessage());	
		}

		return mv;
	}
}
